<%+header%>

<script type="text/javascript">//<![CDATA[


  String.prototype.startsWith = function (str){
    return this.indexOf(str) == 0;
  };

	function gotopath(obj)
	{
		document.getElementById("cmd").value ="cd " + obj.innerHTML;
		update_status();

	}
	var iwxhr = new XHR();
	function update_status(e) {
		if(e!=null && e.keyCode!=13) return false;
		/*var cmd = document.getElementById("cmd").value;
		if(cmd.startsWith("cd"))
		{
			newpath = cmd.replace("cd","").replace(" ","");
			if(newpath.startsWith("/")){
				document.getElementById("currentpath").value = newpath;
			}else{
				document.getElementById("currentpath").value += newpath;
			}
			cmd="";
		}*/
		
	obj = document.getElementById("screen");
	cmdstr=(document.getElementById("cmd").value).replace(/\s+/g,"")
	if (cmdstr == "cls" || cmdstr == "clear")
	{
	   obj = document.getElementById("screen");
	   obj.value = "";
	   document.getElementById("cmd").value = "";
	   return false;	
	}

		iwxhr.get('<%=luci.dispatcher.build_url("admin", "system", "cmd_run")%>', {cmd: document.getElementById("cmd").value,path:document.getElementById("currentpath").value  },
			function(x, ifc)
			{
				obj = document.getElementById("screen");
				obj.value+=ifc[0];
				document.getElementById("currentpath").value=ifc[1];;
				document.getElementById("list").innerHTML="<ul><li onclick='gotopath(this)'>..</li><li onclick='gotopath(this)'>"+ifc[2].replace(/,/g,"</li><li onclick='gotopath(this)'>")+"</li></ul>";
				obj.scrollTop = obj.scrollHeight;
				document.getElementById("cmd").focus();
			}
		)
	


	if ((document.getElementById("cmd").value).replace(/\s+/g,"") != "")
	  {
		obj.value+="\r\n--------------------------------------------\r\n"+document.getElementById("cmd").value+"\r\n--------------------------------------------\r\n";
		document.getElementById("cmd").value = "";
		return false;	
	  }
	};
//]]></script>

<style>
.cmd_line{
	background-color:yellow;

}
#list ul li
{
	margin-right:5px;
	height:16px;
	display:block;
	float:left;
	word-break: keep-all;
}
#list ul li:hover
{
	background:blue;
	cursor:pointer;
}
</style>
<div class="cbi-map" id="cbi-system"><h2><a id="content" name="content">Webshell</a></h2><div class="cbi-map-descr"><%=translate("Do not perform ping, top and other non-immediate return of the command, not causing luci hang!")%> </div>


<textarea id="screen" style="width:100%;height:400px;overflow:auto;font-size:12px;"></textarea>
<%=translate("command: ")%>
<input type="text" id="cmd" onkeypress="update_status(event)"/>
<br/>
<%=translate("Press the Enter key to execute")%>
<br/>
<br/>
<%=translate("Working Path:")%>
<input type="text" id="currentpath" style="width:400px;" value="/"/>

<div><strong><%=translate("Files List:")%></strong></div>
<div id="list"></div>
<script>
   update_status(null);
</script>
<%+footer%>